﻿using Confluent.Kafka;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace HECore.Kafka_Consumer.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        private readonly KafkaDependentProducer<string, string> producer;

        public AccountController(KafkaDependentProducer<string, string> producer)
        {
            this.producer = producer;
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <returns></returns>
        [HttpGet("register")]
        public async void Register()
        {
            var topic = KafkaTopTicEnum.account_register_topic.ToString();
            var user = new UserModel() { Id = Guid.NewGuid(), Name = "User-" + Guid.NewGuid().ToString("N"), DateTime = DateTime.Now, Handle = "注册" };
            await this.producer.ProduceAsync(topic, new Message<string, string> { Key = user.Id.ToString(), Value = JsonConvert.SerializeObject(user) });
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        [HttpGet("login")]
        public async void Login()
        {
            var topic = KafkaTopTicEnum.account_login_topic.ToString();
            var user = new UserModel() { Id = Guid.NewGuid(), Name = "User-" + Guid.NewGuid().ToString("N"), DateTime = DateTime.Now, Handle = "登录" };
            await this.producer.ProduceAsync(topic, new Message<string, string> { Key = user.Id.ToString(), Value = JsonConvert.SerializeObject(user) });
        }

        /// <summary>
        /// 用户编辑
        /// </summary>
        /// <returns></returns>
        [HttpGet("edit")]
        public async void Edit()
        {
            var topic = KafkaTopTicEnum.account_edit_topic.ToString();
            var user = new UserModel() { Id = Guid.NewGuid(), Name = "User-" + Guid.NewGuid().ToString("N"), DateTime = DateTime.Now, Handle = "编辑" };
            await this.producer.ProduceAsync(topic, new Message<string, string> { Key = user.Id.ToString(), Value = JsonConvert.SerializeObject(user) });
        }

        /// <summary>
        /// 用户注销
        /// </summary>
        /// <returns></returns>
        [HttpGet("logout")]
        public async void Logout()
        {
            var topic = KafkaTopTicEnum.account_logout_topic.ToString();
            var user = new UserModel() { Id = Guid.NewGuid(), Name = "User-" + Guid.NewGuid().ToString("N"), DateTime = DateTime.Now, Handle = "注销" };
            await this.producer.ProduceAsync(topic, new Message<string, string> { Key = user.Id.ToString(), Value = JsonConvert.SerializeObject(user) });
        }
    }

    public class UserModel
    {
        /// <summary>
        /// Id
        /// </summary>
        public Guid Id { get; set; }
        
        /// <summary>
        /// Name
        /// </summary>
        public string? Name { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime DateTime { get; set; }

        /// <summary>
        /// 操作
        /// </summary>
        public string? Handle {  get; set; }
    }
}
